package com.sali.b_design_pattern.f_responsibility_pattern.strategy.method01;

import com.sali.design_pattern.d_strategy_pattern.lesson_03.utils.SpringContextUtil;
import com.sali.design_pattern.f_responsibility_pattern.service.IResponseUserService;

public abstract class JobHandler {


    protected IResponseUserService responseUserService = SpringContextUtil.getBean(IResponseUserService.class);


    protected JobHandler chain;
    protected JobHandlerParams params;

    public JobHandler(JobHandlerParams params) {
        this.params = params;
    }

    // 设置下一个执行节点
    public void setNext(JobHandler jobHandler) {
        this.chain = jobHandler;
    }

    // 具体执行逻辑
    public abstract void invokeHandler();

    // 具体执行逻辑
    public void doHandler() {
        invokeHandler();
        if (null != chain) {
            chain.doHandler();
        }
    }

    // handler建造者
    public static class Builder {
        // 头节点
        private JobHandler head;
        // 尾节点
        private JobHandler tail;

        public Builder addHandler(JobHandler jobHandler) {
            if (null == this.head) {
                this.head = this.tail = jobHandler;
                return this;
            }
            this.tail.setNext(jobHandler);
            this.tail = jobHandler;
            return this;
        }

        public JobHandler build() {
            return this.head;
        }
    }

}
